<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\ProductPickup;

use App\Models\ProductPickup\PickupOrder;
use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class PickupOrderBatch extends BatchAction{

    public $name = '批量申请';

    public function handle(Collection $collection, Request $request){

        try{
            $count = 0;
            $userId = Admin::user()->id;
            foreach ($collection as $model) {
                //if(!in_array($model->status, [1,3])){
                //    continue;
                //}
                $model->seller_id = $userId;
                $model->status = PickupOrder::STATUS_YES; //申请中
                $flag = $model->save();
                $flag && $count++;
            }
            return $this->response()->success("成功申请{$count}条记录！")->refresh();

        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }

    public function dialog(){
        $this->confirm('确定申请备货？');
    }
}
